<?php
/**
 * Put class, perform the put command
 * @package action
 * @author alcaitiff
 */
class Put implements Action{

    /**
     * @static
     * @var integer
     **/
	static private $tick;
    /**
     * @static
     * @var Char
     **/
	static private $char;
    /**
    * Usage: put [number|"all"] Item [number] container [number]
	*
    * The container may be on hands or floor, the item must be on your hands or on floor
	* @static
    * @param Command $command Command
    * @param int $tick Tick
    * @return String This command will return a msg
    */
	static public function run($command,$tick){
		self::$tick=$tick;
		self::$char=$command->character;
        $parameters=$command->getArrangedParameters(2,5,true);
		if($parameters==NULL || !isset($parameters[3])){
            MessageSystem::addErrorMessage('Invalid Parameters');
            return null;
        }else{
            return self::putItem($parameters[0], $parameters[1], $parameters[2], $parameters[3], $parameters[4]);
        }
	}
    /**
     * put an item in a container
     * @param int|string $num
     * @param string $name
     * @param int $index
     * @param string $containerName
     * @param int $containerIndex
     * @return string
     */
    static private function putItem($num,$name,$index,$containerName,$containerIndex){
        $item=null;
        $container=null;
        if($index>1){
            $errorMessage=$name.'['.$index.'] not found';
        }else{
            $errorMessage=$name.' not found';
        }
        $container=self::$char->getItem($containerName, $containerIndex);
        if($container!==NULL){
            if($container->container!=null){
                $item=self::$char->getItem($name, $index);
                if($item!=null && $item->getId()==$container->getId()){
                    MessageSystem::addErrorMessage('You cant put an item inside of itself');
                }elseif($item!=null){
                    return self::$char->putItemOnContainer($item,$num,$container);
                }
            }else{
                MessageSystem::addErrorMessage($containerName.' is not a container');
            }
        }
        return null;
    }

}